//import mx.events.EventDispatcher;
import com.dmotiko.lib.EventDispatcherPro;
import com.dmotiko.util.XMLUtils;
import mx.utils.Delegate;
import com.dmotiko.general.*

class com.dmotiko.general.DataXMLLoader {
	
	public static var DATA_LOADED:String = "dataxmlloader_dataloaded";
	public static var DATA_ERROR:String = "dataxmlloader_dataerror";
	
	public var addEventListener:Function;
	public var removeEventListener:Function;
	public var dispatchEvent:Function;
	
	private var data:Object;
	private var src:String;
	private var xml:XML;
		
	public function DataXMLLoader(src:String){
		EventDispatcherPro.initialize(this);
		if (src) run(src);
	}
	public function run(src){
		var root = this;
		this.src = src;
		xml = new XML();
		xml.ignoreWhite = true;
		xml.load(src);
		
		xml.onLoad = function( success:Boolean ){
			if( success ){
				root.parserFunction();
			} else {
				root.dispatchEvent( { target: root, type: DATA_ERROR, data: root.data, src: root.src } );
			}
		}
		
	}
	
	/**
	 *  @author: sminutoli
	 *  @usage: for override purposes
	 */
	
	private function parserFunction(){
		var nodes:Array = xml.firstChild.childNodes;
		this.data = XMLUtils.node2object( xml.firstChild );
		loadEnd();
		return;
	}
	
	private function loadEnd(){
		dispatchEvent( { target: this, type: DATA_LOADED, data: this.data, src: this.src } );
	}
	
}